package com.borya.action.mobile;

import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.borya.action.BaseAction;
import com.borya.framework.annotation.Action;
import com.borya.framework.annotation.Autowired;
import com.borya.framework.annotation.JsonResult;
import com.borya.framework.annotation.UrlMapping;
import com.borya.framework.core.ServerRequest;
import com.borya.framework.core.ServerResponse;
import com.borya.model.db.User;
import com.borya.model.entity.ApplyRecord;
import com.borya.service.mobile.MerchantService;
import com.borya.statuscode.StatusCode;

@UrlMapping("/c/merchant/")
@Action
public class MerchantAction extends BaseAction {

	private Logger log = Logger.getLogger(getClass());

	@Autowired("merchantServiceImpl")
	private MerchantService merchantService;

	@UrlMapping("getInfo")
	@JsonResult
	public String getInfo(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		String result = merchantService.getInfo(getUser(request), json);
		log.info("INFO[login" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("popChannelList")
	@JsonResult
	public String popChannelList(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		String result = merchantService.popChannelList(getUser(request), json);
		log.info("INFO[login" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("userInfo")
	@JsonResult
	public String userInfo(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		String result = merchantService.userInfo(getUser(request), json);
		log.info("RECV [" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("applyPower")
	@JsonResult
	public String applyPower(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		String result = merchantService.applyPower(getUser(request), json);
		json.put("img", "");
		log.info("applyPower[login" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("getScopes")
	@JsonResult
	public String getScopes(ServerRequest request, ServerResponse response) {
		try {
			JSONObject json = getJson(request);
			// 参数需有营运商，""表示全部
			if (!json.containsKey("type")) {
				log.warn("param img error");
				return StatusCode.PARAM_ERROR.toJSON("营运商类型必传");
			}
			String result = merchantService.getScopesNew(getUser(request), json);
			log.info("getScopes[login" + json + "],OUT[" + result + "]");
			return result;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return StatusCode.SYSTEM_EXCEPTION.json().toString();
		}
	}

	@UrlMapping("applyScope")
	@JsonResult
	public String applyScope(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		// 参数必须包含 营运商、申请地Id
		if (!json.containsKey("type") || !json.containsKey("scopeId")) {
			log.warn("param img error");
			return StatusCode.PARAM_ERROR.toJSON();
		}
		String result = merchantService.applyScope(getUser(request), json);
		log.info("applyScope[login" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("getApplyRecords")
	@JsonResult
	public String getApplyRecords(ServerRequest request, ServerResponse response) {
		try {
			JSONObject json = getJson(request);
			log.info("RECV:【" + json + "】");

			// 参数校验
			int pageSize = json.getIntValue("pageSize");
			if (pageSize < 1) {
				log.warn("query pageSize error");
				return StatusCode.PARAM_ERROR.toJSON("参数错误，页条数");
			}
			int pageIndex = json.getIntValue("pageIndex");
			if (pageIndex < 0) {
				log.warn("query pageIndex error");
				return StatusCode.PARAM_ERROR.toJSON("参数错误，当前页码数");
			}
			List<ApplyRecord> advices = merchantService.getApplyRecords(getUser(request).getDealerId(), pageSize,
					pageIndex);
			if (advices != null) {
				log.info("OUT【" + advices.toString() + "】");
				String jsonAdvices = JSON.toJSONString(advices, SerializerFeature.WriteNonStringValueAsString);
				return StatusCode.SUCCESS.toDiyJson(true, "data", jsonAdvices);
			} else {
				log.error("DB process error");
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return StatusCode.SYSTEM_EXCEPTION.toJSON();
	}

	@UrlMapping("shqpSign")
	@JsonResult
	public String get(ServerRequest request, ServerResponse response) {
		String r = merchantService.setImg(getUser(request), getJson(request), 1);
		log.info("get return : " + r);
		return r;
	}

	@UrlMapping("shqpRenew")
	@JsonResult
	public String shqpRenew(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		log.info("shqpRenew get:" + json);
		String result = merchantService.shqpRenew(getUser(request), json);
		log.info("shqpRenew OUT:" + result);
		return result;
	}

	@UrlMapping("applyChangeAddress")
	@JsonResult
	public String applyChangeAddress(ServerRequest request, ServerResponse response) {
		User user = getUser(request);
		log.info("REV[" + getJson(request) + "]");
		String result = merchantService.applyChangeAddress(user, getJson(request));
		log.info("OUT[" + result + "]");
		return result;
	}

	@UrlMapping("getAddress")
	@JsonResult
	public String getAddress(ServerRequest request, ServerResponse response) {
		User user = getUser(request);
		log.info("RECV[" + user + "]");
		String result = merchantService.getAddress(user);
		log.info("OUT[" + result + "]");
		return result;
	}

	/**
	 * 获取商户推广权限(接口移动到xums)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@Deprecated
	@UrlMapping("getSpreadAuthority")
	@JsonResult
	public String getSpreadAuthority(ServerRequest request, ServerResponse response) {
		User user = getUser(request);
		log.info("RECV[" + user + "]");
		String result = merchantService.getSpreadAuthority(user);
		log.info("OUT[" + result + "]");
		return result;
	}

}